import { CascaderOption } from "antd-mobile/es/components/cascader";

// 这里写入功能函数

/**
 * @name: getListToOptions
 * @msg: 将接收到的模板数据的数据 转化为 CascaderOption 格式
 * @param {eventArray:T[]}  要处理的数组传入泛型
 * @param {fn:(x:T)=>CascaderOption}  处理的函数
 * @return:
 */

export function getListToOptions<T>(
  eventArray: T[],
  fn: (x: T) => CascaderOption
): CascaderOption[] {
  const data = eventArray.map((item) => {
    return fn(item);
  });
  return data;
}

export function getParamsByUrl<T>(url: string): T | {} {
  const res: T | {} = {};
  if (typeof url !== "string") return res;
  const r = url.trim().substring(1).split("&");
  r.forEach((item) => {
    const params = item.split("=");
    res[params[0]] = params[1];
  });
  return res;
}

export function htmlUnEscape(str: string) {
  if (str) {
    return str.replace(/\$#39;|&lt;|&gt;|&quot;|&amp;/g, (match) => {
      switch (match) {
        case "&lt;":
          return "<";
        case "&gt;":
          return ">";
        case "&quot;":
          return '"';
        case "&amp;":
          return "&";
        case "$#39;":
          return "\\";
      }
    });
  }
  return "";
}
